using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using FluentAvalonia.UI.Controls;
using LearnTodo.ViewModels;

namespace LearnTodo.Views;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Frame.IsNavigationStackEnabled = true;
        Frame.NavigationPageFactory = new NavigationFactory();
        Frame.NavigateFromObject("Home");
    }

    private async void AddTodoClick(object? sender, RoutedEventArgs e)
    {
        var td = new TaskDialog
        {
            Title = "LearnTodo",
            Header = "添加待办",
            IconSource = new SymbolIconSource(){Symbol = Symbol.Add},
            Content = new AddTodoDialog(),
            FooterVisibility = TaskDialogFooterVisibility.Never,
            Buttons =
            {
                TaskDialogButton.OKButton,
                TaskDialogButton.CancelButton
            },
            XamlRoot = (Visual)VisualRoot!
        };
        td.Closing += (dialog, args) =>
        {
            if ((TaskDialogStandardResult)args.Result != TaskDialogStandardResult.OK) return;
            if (dialog.Content is not AddTodoDialog d) return;
            var result = d.Done();
            if (result == null)
            {
                args.Cancel = false;
                return;
            }

            if (DataContext is not MainWindowViewModel model) return;
            model.Todos.Add(result);
        };
        await td.ShowAsync();
    }

    private void BackClick(object? sender, RoutedEventArgs e)
    {
        Frame.GoBack();
    }

    private void SelectionChanged(object? sender, SelectionChangedEventArgs e)
    {
        if(sender is not ListBox box)return;
        Frame.NavigateFromObject(box.SelectedValue);
    }
}